package com.lb.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class SpringContextUtils implements ApplicationContextAware {
	public static ApplicationContext applicationContext;

	private static ConfigurableApplicationContext configurableContext;
	private static DefaultListableBeanFactory beanDefinitionRegistry;


	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtils.applicationContext = applicationContext;
		configurableContext = (ConfigurableApplicationContext) SpringContextUtils.applicationContext;
		beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext.getBeanFactory();
	}
	/**
	 * 根据名称注册容器对象
	 */
	public static void registerBeanNew(String beanName, Class<?> beanClass) {
		GenericBeanDefinition bd = new GenericBeanDefinition();
		bd.setBeanClass(beanClass);
		if (StringUtils.hasText(beanName)) {
			beanDefinitionRegistry.registerBeanDefinition(beanName, bd);
		} else {
			BeanDefinitionReaderUtils.registerWithGeneratedName(bd, beanDefinitionRegistry);
		}

	}
	/**
	 * 根据类型注册容器对象
	 */
	public static void registerBean(Class<?> beanClass) {
		registerBeanNew(null, beanClass);
	}

	/**
	 * 根据名称注册容器对象
	 */
	public static void registerBean(String beanName, Class requiredType) {
		BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
				.genericBeanDefinition(requiredType.getName());
		BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
		beanDefinition.setScope("singleton");
		beanDefinition.setLazyInit(false); // 设置是否懒加载
		beanDefinition.setAutowireCandidate(true);
		beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition);
	}

	/**
	 * 根据名称获取容器对象
	 */
	public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}
	/**
	 * 根据类型获取容器对象
	 */
	public static Object getBean(Class  requiredType) {
		return applicationContext.getBean(requiredType);
	}
	/**
	 * 根据名称反射获取容器对象
	 */
	public static <T> T getBean(String name, Class<T> requiredType) {
		return applicationContext.getBean(name, requiredType);
	}
	/**
	 * 是否含有容器对象
	 */
	public static boolean containsBean(String name) {
		return applicationContext.containsBean(name);
	}
	/**
	 * 是否单例容器对象
	 */
	public static boolean isSingleton(String name) {
		return applicationContext.isSingleton(name);
	}
	/**
	 * 根据名称获取容器对象的反射类型
	 */
	public static Class<? extends Object> getType(String name) {
		return applicationContext.getType(name);
	}

	/**
	 * 根据名称移除容器对象
	 */
    public static void unregisterBean(String beanName){
        beanDefinitionRegistry.removeBeanDefinition(beanName);
    }



}